package cn.com.nes.site.action.user;

import cn.com.nes.common.util.*;
import cn.com.nes.common.wx.WeChatCommonUtils;
import cn.com.nes.common.wx.WxAuth;
import cn.com.nes.mybatis.agent.system.entity.WechatProgramConfig;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.UserInfo;
import cn.com.nes.site.module.service.H5Service;
import cn.com.nes.site.module.service.UserService;
import cn.com.nes.site.module.service.crm.ClueService;
import cn.com.nes.site.service.system.wechatProgram.WechatProgramConfigService;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Map;

@Slf4j
@Controller("wx")
@RequestMapping("/wx")
public class WxAction {
	
	@Resource
	private H5Service service;
	
	@Resource
	private ClueService service_clue;

	@Resource
	private UserService userservice;

	@Resource
	private WechatProgramConfigService wechatProgramConfigService;

	//热恋云头像上传本地
	@RequestMapping(method = RequestMethod.POST, value = "/addUsersimg")
	public @ResponseBody  PageData addUsersimg(HttpServletRequest request,
											   HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		StringBuffer sb = new StringBuffer();
		Long timeStamp = System.currentTimeMillis();  //获取当前时间戳
		String fileName = timeStamp+".jpg";
		try {
			//下面这句必须加，不然报错
			MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			//获取一起跟文件传过来的其他参数值
			//String id = multipartRequest.getParameter("id");
			//获取上传上来的文件
			Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
			for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
				MultipartFile file = entry.getValue();
				String rootPath = System.getProperty("catalina.home");
				sb.append(rootPath).append(File.separator).append("webapps").append(File.separator).append("ROOT").append(File.separator).append("wxphoto").append(File.separator);
				String path = sb.toString();
				file.transferTo(new File(path));//上传到本地
			}
			res.setKey(fileName);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	//热恋云头像上传oss
	@RequestMapping(method = RequestMethod.POST, value = "/addUsersimgOss")
	public @ResponseBody  PageData addUsersimgOss(HttpServletRequest request,
												  HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		StringBuffer sb = new StringBuffer();
		try {
			String fileName = "";
			//下面这句必须加，不然报错
			MultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			//获取一起跟文件传过来的其他参数值
			//String id = multipartRequest.getParameter("id");
			//获取上传上来的文件
			Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
			for (Map.Entry<String, MultipartFile> entry : fileMap.entrySet()) {
				MultipartFile file = entry.getValue();
				String name=file.getOriginalFilename();
				String type=name.substring(name.lastIndexOf("."));
				fileName ="crm/wxphoto/"+ System.currentTimeMillis()+type;
				OssUtil.fileUpload(type,fileName,file);//上传到OSS
			}
			res.setKey(fileName);
			res.setRetCode(0);
			res.setRetMessage("OK");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	//热恋云查询资料
	@RequestMapping(method = RequestMethod.GET, value = "/getFileList")
	public @ResponseBody  PageData getFileList(HttpServletRequest request,
											   HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		DataReq req = req_Status.getReq() ;
		res = service.getFileList(req) ;
		return res;
	}

//	@RequestMapping(method = RequestMethod.POST, value = "/addUsers")
//	public @ResponseBody  PageData addUsers(HttpServletRequest request,
//		HttpServletResponse response, ModelMap modelMap) throws Exception {
//		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
//		PageData res = new PageData() ;
//		if (req_Status.getRetCode() != 0) {
//			res = new PageData();
//			res.setRetCode(req_Status.getRetCode()) ;
//			res.setRetMessage(req_Status.getRetMessage());
//			return res;
//		}
//		DataReq req = req_Status.getReq() ;
//		PageData restmp = service_clue.getActivityByID(req) ;
//		if(restmp.getRetCode()==0) {
//			Map<String, Object> item = restmp.getData() ;
//			if("1".equals(restmp.getKey())) {
//				req.getReqData().put("companyId","45") ;
//			}else {
//				req.getReqData().put("companyId", item.get("companyId")) ;
//			}
//			req.getReqData().put("createUid", item.get("createUid")) ;
//			req.getReqData().put("cluesource", item.get("cluesource")) ;
//			req.getReqData().put("activity_uuid", item.get("uuid")) ;
//		}
//
//		if(StringUtils.isEmpty((String)req.getReqData().get("companyId"))) {
//			restmp = service.getWxUser(req) ;
//			if(restmp.getRetCode()==0 && restmp.getPage().size()>0) {
//				Map<String, Object> item = restmp.getPage().get(0) ;
//				req.getReqData().put("companyId", item.get("companyId")) ;
//				req.getReqData().put("createUid", item.get("createUid")) ;
//			}
//		}
//
//		String code = (String)req.getReqData().get("verCode") ;
//		String phone = (String)req.getReqData().get("phone") ;
//
//		if(!StringUtils.isEmpty(code)) {
//			if(!ValidCodeUtil.validCode(phone, code)) {
//				res.setRetCode(1) ;
//				res.setRetMessage("验证码错误！");
//				return res ;
//			}
//		}
//
//		String encryptedData = (String) req.getReqData().get("encryptedData") ;
//		if(!StringUtils.isEmpty(encryptedData)) {
//			String sessionKey = (String) req.getReqData().get("sessionKey") ;
//			String iv = (String) req.getReqData().get("iv") ;
//			JSONObject jsonObject = WeChatCommonUtils.getUserInfo(encryptedData, sessionKey, iv);
//			req.getReqData().put("phone", jsonObject.getString("phoneNumber")) ; //跟进状态
//		}
//
//		if(StringUtils.isEmpty((String)req.getReqData().get("companyId"))) {//如果找不到公司，默认本公司
//			req.getReqData().put("companyId", "45") ;
//		}
//
//		if(StringUtils.isEmpty((String)req.getReqData().get("createUid"))) {//如果找不到公司，默认管理员
//			req.getReqData().put("createUid", "131") ;
//		}
//
//		if(StringUtils.isEmpty((String)req.getReqData().get("cluesource"))) {//默认来源，07（其他）
//			req.getReqData().put("cluesource", "07") ;
//		}
//
//		req.getReqData().put("active_info", req.getReqData()) ;
//
//		UserInfo uinfo = new UserInfo();
//		uinfo.setId((String)req.getReqData().get("createUid"));
//		uinfo.setCompanyId((String)req.getReqData().get("companyId"));
//		req.setUinfo(uinfo );
//		req.getReqData().put("isLog", "0") ;
//		req.getReqData().put("openid", req.getReqData().get("openId")) ;
//		if(StringUtils.isEmpty((String) req.getReqData().get("name"))) {
//			req.getReqData().remove("name");
//		}
//		res = service.addUsers(req) ;
//		if(res.getRetCode()==0) {
//			req.getReqData().put("followupstatus", "01") ; //跟进状态
//			req.getReqData().put("poolentrymode", "05") ; //线索池
//			req.getReqData().put("culestate", "02") ; //线索状态，新线索
//			req.getReqData().put("cluestage", "01") ; //跟进状态
//			req.getReqData().put("cluesource", req.getReqData().get("cluesource")) ; //跟进状态
//			req.getReqData().put("pooltype",  "其他") ;
//			String xzqhdm = "86" ;//service_clue.getAreaCodeByPhone(req);
//			req.getReqData().put("xzqhdm",xzqhdm) ;
//			req.getReqData().put("advertisementId",req.getReqData().get("activity_uuid")) ;
//			req.getReqData().put("cluename", req.getReqData().get("companyName")) ;
//			req.getReqData().put("cluebelongtocompany", req.getReqData().get("companyId")) ;
//			req.getReqData().put("projectname", req.getReqData().get("companyName")) ;
//			req.getReqData().put("companycontact",req.getReqData().get("name")) ;
//			req.getReqData().put("cluephone",req.getReqData().get("phone") ) ;
//			req.getReqData().put("projectaddress",null) ;
//			req.getReqData().put("numberhome", req.getReqData().get("province")) ;
//			req.getReqData().put("uuid",null) ;
//			if(!StringUtils.isEmpty((String) req.getReqData().get("name"))) {
//				restmp = service_clue.addClue(req) ;
//			}
//		}
//
//		PageData resTmp = service.getActiveUser(req) ;
//		if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
//			res.setData(resTmp.getPage().get(0));
//		}
//		return res;
//	}

	//热恋云小程序登录
	@RequestMapping(method = RequestMethod.POST, value = "/addUsers")
	public @ResponseBody  PageData addUsers(HttpServletRequest request,
											HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		DataReq req = req_Status.getReq() ;
		//获取大屏关联活动信息，登录时没有关联活动存00，公司代码存0000（创清），线索来源存07（热恋云小程序）
		PageData restmp = service_clue.getActivityByID(req) ;
		if(restmp.getRetCode()==0) {
			Map<String, Object> item = restmp.getData() ;
			req.getReqData().put("companyId", item.get("companyId")) ;
			req.getReqData().put("createUid", item.get("createUid")) ;
			req.getReqData().put("cluesource", item.get("cluesource")) ;
			req.getReqData().put("activity_uuid", item.get("uuid")) ;
		}
		//手机号验证码验证--app用
		String code = (String)req.getReqData().get("verCode") ;
		String phone = (String)req.getReqData().get("phone") ;
		if(!StringUtils.isEmpty(code)) {
			if(!ValidCodeUtil.validCode(phone, code)) {
				res.setRetCode(1) ;
				res.setRetMessage("验证码错误！");
				return res ;
			}
		}
		//解析手机号
		String encryptedData = (String) req.getReqData().get("encryptedData") ;
		if(!StringUtils.isEmpty(encryptedData)) {
			String sessionKey = (String) req.getReqData().get("sessionKey") ;
			String iv = (String) req.getReqData().get("iv") ;
			JSONObject jsonObject = WeChatCommonUtils.getUserInfo(encryptedData, sessionKey, iv);
			req.getReqData().put("phone", jsonObject.getString("phoneNumber")) ;
		}
		req.getReqData().put("active_info", req.getReqData()) ;
		//插入用户信息
		UserInfo uinfo = new UserInfo();
		uinfo.setId((String)req.getReqData().get("createUid"));
		uinfo.setCompanyId((String)req.getReqData().get("companyId"));
		req.setUinfo(uinfo);
		req.getReqData().put("isLog", "0") ;
		req.getReqData().put("openid", req.getReqData().get("openId")) ;
		if(StringUtils.isEmpty((String) req.getReqData().get("name"))) {
			req.getReqData().remove("name");
		}
		res = service.addUsers(req) ;
		//插入线索信息
		if(res.getRetCode()==0) {
			req.getReqData().put("followupstatus", "01") ; //跟进状态
			req.getReqData().put("poolentrymode", "05") ; //线索池
			req.getReqData().put("culestate", "02") ; //线索状态，新线索
			req.getReqData().put("cluestage", "01") ; //跟进状态
			req.getReqData().put("pooltype",  "其他") ;
			String xzqhdm = "86" ;//service_clue.getAreaCodeByPhone(req);
			req.getReqData().put("xzqhdm",xzqhdm) ;
			req.getReqData().put("advertisementId",req.getReqData().get("activity_uuid")) ;
			req.getReqData().put("cluename", req.getReqData().get("companyName")) ;
			req.getReqData().put("cluebelongtocompany", req.getReqData().get("companyId")) ;
			req.getReqData().put("projectname", req.getReqData().get("companyName")) ;
			req.getReqData().put("companycontact",req.getReqData().get("name")) ;
			req.getReqData().put("cluephone",req.getReqData().get("phone") ) ;
			req.getReqData().put("projectaddress",null) ;
			req.getReqData().put("numberhome", req.getReqData().get("province")) ;
			req.getReqData().put("uuid",null) ;
			if(!StringUtils.isEmpty((String) req.getReqData().get("name"))) {
				restmp = service_clue.addClue(req) ;
			}
		}
		//查询用户信息
		PageData resTmp = service.getActiveUser(req) ;
		if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
			res.setData(resTmp.getPage().get(0));
		}
		return res;
	}

	//昊城小程序登录
	@RequestMapping(method = RequestMethod.POST, value = "/addUsers_hc")
	public @ResponseBody  PageData addUsers_hc(HttpServletRequest request,
											HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		DataReq req = req_Status.getReq() ;
		//获取大屏关联活动信息，登录时没有关联活动存00，公司代码存0000（创清），线索来源存07（热恋云小程序）
		PageData restmp = service_clue.getActivityByID(req) ;
		if(restmp.getRetCode()==0) {
			Map<String, Object> item = restmp.getData() ;
			req.getReqData().put("companyId", item.get("companyId")) ;
			req.getReqData().put("createUid", item.get("createUid")) ;
			req.getReqData().put("cluesource", item.get("cluesource")) ;
			req.getReqData().put("activity_uuid", item.get("uuid")) ;
		}
		req.getReqData().put("active_info", req.getReqData()) ;
		//插入用户信息
		UserInfo uinfo = new UserInfo();
		uinfo.setId((String)req.getReqData().get("createUid"));
		uinfo.setCompanyId((String)req.getReqData().get("companyId"));
		req.setUinfo(uinfo);
		req.getReqData().put("isLog", "0") ;
		req.getReqData().put("openid", req.getReqData().get("openId")) ;
		if(StringUtils.isEmpty((String) req.getReqData().get("name"))) {
			req.getReqData().remove("name");
		}
		res = service.addUsers(req) ;
		//插入线索信息
		if(res.getRetCode()==0) {
			req.getReqData().put("followupstatus", "01") ; //跟进状态
			req.getReqData().put("poolentrymode", "05") ; //线索池
			req.getReqData().put("culestate", "02") ; //线索状态，新线索
			req.getReqData().put("cluestage", "01") ; //跟进状态
			req.getReqData().put("pooltype",  "其他") ;
			String xzqhdm = "86" ;//service_clue.getAreaCodeByPhone(req);
			req.getReqData().put("xzqhdm",xzqhdm) ;
			req.getReqData().put("advertisementId",req.getReqData().get("activity_uuid")) ;
			req.getReqData().put("cluename", req.getReqData().get("companyName")) ;
			req.getReqData().put("cluebelongtocompany", req.getReqData().get("companyId")) ;
			req.getReqData().put("projectname", req.getReqData().get("companyName")) ;
			req.getReqData().put("companycontact",req.getReqData().get("name")) ;
			req.getReqData().put("cluephone",req.getReqData().get("phone") ) ;
			req.getReqData().put("projectaddress",null) ;
			req.getReqData().put("numberhome", req.getReqData().get("province")) ;
			req.getReqData().put("uuid",null) ;
			if(!StringUtils.isEmpty((String) req.getReqData().get("name"))) {
				restmp = service_clue.addClue(req) ;
			}
		}
		//查询用户信息
		PageData resTmp = service.getActiveUser(req) ;
		if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
			res.setData(resTmp.getPage().get(0));
		}
		return res;
	}

	@RequestMapping(method = RequestMethod.POST, value = "/addUsers_shop")
	public @ResponseBody  PageData addUsers_shop(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		DataReq req = req_Status.getReq() ;
		//获取大屏关联活动信息，登录时没有关联活动存00，公司代码存0000（创清），线索来源存07（热恋云小程序）
		PageData restmp = service_clue.getActivityByID(req) ;
		if(restmp.getRetCode()==0) {
			Map<String, Object> item = restmp.getData() ;
			req.getReqData().put("companyId", item.get("companyId")) ;
			req.getReqData().put("createUid", item.get("createUid")) ;
			req.getReqData().put("cluesource", item.get("cluesource")) ;
			req.getReqData().put("activity_uuid", item.get("uuid")) ;
		}
		req.getReqData().put("active_info", req.getReqData()) ;
		//插入用户信息
		UserInfo uinfo = new UserInfo();
		uinfo.setId((String)req.getReqData().get("createUid"));
		uinfo.setCompanyId((String)req.getReqData().get("companyId"));
		req.setUinfo(uinfo);
		req.getReqData().put("isLog", "0") ;
		req.getReqData().put("openid", req.getReqData().get("openId")) ;
		if(StringUtils.isEmpty((String) req.getReqData().get("name"))) {
			req.getReqData().remove("name");
		}
		res = service.addUsers(req) ;
		//插入线索信息
		if(res.getRetCode()==0) {
			req.getReqData().put("followupstatus", "01") ; //跟进状态
			req.getReqData().put("poolentrymode", "05") ; //线索池
			req.getReqData().put("culestate", "02") ; //线索状态，新线索
			req.getReqData().put("cluestage", "01") ; //跟进状态
			req.getReqData().put("pooltype",  "其他") ;
			String xzqhdm = "86" ;//service_clue.getAreaCodeByPhone(req);
			req.getReqData().put("xzqhdm",xzqhdm) ;
			req.getReqData().put("advertisementId",req.getReqData().get("activity_uuid")) ;
			req.getReqData().put("cluename", req.getReqData().get("companyName")) ;
			req.getReqData().put("cluebelongtocompany", req.getReqData().get("companyId")) ;
			req.getReqData().put("projectname", req.getReqData().get("companyName")) ;
			req.getReqData().put("companycontact",req.getReqData().get("name")) ;
			req.getReqData().put("cluephone",req.getReqData().get("phone") ) ;
			req.getReqData().put("projectaddress",null) ;
			req.getReqData().put("numberhome", req.getReqData().get("province")) ;
			req.getReqData().put("uuid",null) ;
			if(!StringUtils.isEmpty((String) req.getReqData().get("name"))) {
				restmp = service_clue.addClue(req) ;
			}
		}
		//查询用户信息
		PageData resTmp = service.getActiveUser(req) ;
		if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
			res.setData(resTmp.getPage().get(0));
		}
		return res;
	}


	//热恋云小程序登录
	@RequestMapping(method = RequestMethod.GET, value = "/getOpenIdAndSessionKey")
	public @ResponseBody  PageData getOpenIdAndSessionKey(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			String code = (String) req.getReqData().get("code") ;
			String json = WeChatCommonUtils.getOpenIdAndSessionKey(code);
			WxAuth wxAuth = (WxAuth)JsonUtil.getObjFromStr(json, WxAuth.class) ;
			if (wxAuth.getErrcode()!= 0) {
				res.setRetCode(9);
				res.setRetMessage(wxAuth.getErrmsg());
				System.out.println("ERROR="+JsonUtil.getStriFromObj(wxAuth));
				return res;
		    }
			res.setRetCode(0);
			res.setRetMessage("OK");
			wxAuth.setReg(0);
			req.getReqData().put("openid", wxAuth.getOpenid()) ;
			req.getReqData().put("unionid", wxAuth.getUnionid()) ;
			PageData resTmp = service.getActiveUser(req) ;
			if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
				wxAuth.setReg(1);
				res.setData(resTmp.getPage().get(0));
				//获取商城token
				req.getReqData().put("phone", res.getData().get("phone")) ;
				PageData shopRes = userservice.getShopToken(req) ;
				String shopToken = (String) shopRes.getData().get("shopToken") ;
				res.setKey(shopToken);
			}
			res.setExtData(wxAuth);
			System.out.println(JsonUtil.getStriFromObj(res));
			return res ;
	}

	@RequestMapping(method = RequestMethod.GET, value = "/getOpenIdAndSessionKey_shop")
	public @ResponseBody  PageData getOpenIdAndSessionKey_shop(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		DataReq req = req_Status.getReq() ;
		String code = (String) req.getReqData().get("code") ;
		String json = WeChatCommonUtils.getOpenIdAndSessionKey_shop(code);
		WxAuth wxAuth = (WxAuth)JsonUtil.getObjFromStr(json, WxAuth.class) ;
		if (wxAuth.getErrcode()!= 0) {
			res.setRetCode(9);
			res.setRetMessage(wxAuth.getErrmsg());
			System.out.println("ERROR="+JsonUtil.getStriFromObj(wxAuth));
			return res;
		}
		res.setRetCode(0);
		res.setRetMessage("OK");
		wxAuth.setReg(0);
		req.getReqData().put("openid", wxAuth.getOpenid()) ;
		req.getReqData().put("unionid", wxAuth.getUnionid()) ;
		PageData resTmp = service.getActiveUser(req) ;
		if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
			wxAuth.setReg(1);
			res.setData(resTmp.getPage().get(0));
			//获取商城token
			req.getReqData().put("phone", res.getData().get("phone")) ;
			PageData shopRes = userservice.getShopToken(req) ;
			String shopToken = (String) shopRes.getData().get("shopToken") ;
			res.setKey(shopToken);
		}
		res.setExtData(wxAuth);
		System.out.println(JsonUtil.getStriFromObj(res));
		return res ;
	}

	//昊城小程序登录
	@RequestMapping(method = RequestMethod.GET, value = "/getOpenIdAndSessionKey_hc")
	public @ResponseBody  PageData getOpenID_hc(HttpServletRequest request,
											 HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		DataReq req = req_Status.getReq() ;
		String code = (String) req.getReqData().get("code") ;
		String json = WeChatCommonUtils.getOpenIdAndSessionKey_hc(code);
		WxAuth wxAuth = (WxAuth)JsonUtil.getObjFromStr(json, WxAuth.class) ;
		if (wxAuth.getErrcode()!= 0) {
			res.setRetCode(9);
			res.setRetMessage(wxAuth.getErrmsg());
			System.out.println("ERROR="+JsonUtil.getStriFromObj(wxAuth));
			return res;
		}
		res.setRetCode(0);
		res.setRetMessage("OK");
		wxAuth.setReg(0);
		req.getReqData().put("openid", wxAuth.getOpenid()) ;
		req.getReqData().put("unionid", wxAuth.getUnionid()) ;
		PageData resTmp = service.getActiveUser(req) ;
		if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
			wxAuth.setReg(1);
			res.setData(resTmp.getPage().get(0));
			//获取商城token
			req.getReqData().put("phone", res.getData().get("phone")) ;
			PageData shopRes = userservice.getShopToken(req) ;
			String shopToken = (String) shopRes.getData().get("shopToken") ;
			res.setKey(shopToken);
		}
		res.setExtData(wxAuth);
		System.out.println(JsonUtil.getStriFromObj(res));
		return res ;
	}

	//德尚小程序登录
	@RequestMapping(method = RequestMethod.GET, value = "/getOpenIdAndSessionKey_ds")
	public @ResponseBody  PageData getOpenID_ds(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		DataReq req = req_Status.getReq() ;
		String code = (String) req.getReqData().get("code") ;
		String json = WeChatCommonUtils.getOpenIdAndSessionKey_ds(code);
		WxAuth wxAuth = (WxAuth)JsonUtil.getObjFromStr(json, WxAuth.class) ;
		if (wxAuth.getErrcode()!= 0) {
			res.setRetCode(9);
			res.setRetMessage(wxAuth.getErrmsg());
			System.out.println("ERROR="+JsonUtil.getStriFromObj(wxAuth));
			return res;
		}
		res.setRetCode(0);
		res.setRetMessage("OK");
		wxAuth.setReg(0);
		req.getReqData().put("openid", wxAuth.getOpenid()) ;
		req.getReqData().put("unionid", wxAuth.getUnionid()) ;
		PageData resTmp = service.getActiveUser(req) ;
		if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
			wxAuth.setReg(1);
			res.setData(resTmp.getPage().get(0));
			//获取商城token
			req.getReqData().put("phone", res.getData().get("phone")) ;
			PageData shopRes = userservice.getShopToken(req) ;
			String shopToken = (String) shopRes.getData().get("shopToken") ;
			res.setKey(shopToken);
		}
		res.setExtData(wxAuth);
		System.out.println(JsonUtil.getStriFromObj(res));
		return res ;
	}

	//荣事达小程序登录
	@RequestMapping(method = RequestMethod.GET, value = "/getOpenIdAndSessionKey_rsd")
	public @ResponseBody  PageData getOpenID_rsd(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		// 调用微信接口，获取用户信息
		DataReq req = req_Status.getReq() ;
		String code = (String) req.getReqData().get("code") ;
		String json = WeChatCommonUtils.getOpenIdAndSessionKey_rsd(code);
		WxAuth wxAuth = (WxAuth)JsonUtil.getObjFromStr(json, WxAuth.class) ;
		if (wxAuth.getErrcode()!= 0) {
			res.setRetCode(9);
			res.setRetMessage(wxAuth.getErrmsg());
			System.out.println("ERROR="+JsonUtil.getStriFromObj(wxAuth));
			return res;
		}
		res.setRetCode(0);
		res.setRetMessage("OK");
		wxAuth.setReg(0);
		req.getReqData().put("openid", wxAuth.getOpenid()) ;
		req.getReqData().put("unionid", wxAuth.getUnionid()) ;
		// 通过微信查询接口，获取数据库中用户信息
		PageData resTmp = service.getActiveUser(req) ;
		if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
			wxAuth.setReg(1);
			res.setData(resTmp.getPage().get(0));
			//获取商城token
			req.getReqData().put("phone", res.getData().get("phone")) ;
			PageData shopRes = userservice.getShopToken(req) ;
			String shopToken = (String) shopRes.getData().get("shopToken") ;
			res.setKey(shopToken);
		}
		res.setExtData(wxAuth);
		System.out.println(JsonUtil.getStriFromObj(res));
		return res ;
	}

	// 火燕山小程序登录
	@RequestMapping(method = RequestMethod.GET, value = "/getOpenIdAndSessionKey_hys")
	public @ResponseBody  PageData getOpenID_hys(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		// 调用微信接口，获取用户信息
		DataReq req = req_Status.getReq() ;
		String code = (String) req.getReqData().get("code") ;
		String json = WeChatCommonUtils.getOpenIdAndSessionKey_hys(code);
		WxAuth wxAuth = (WxAuth)JsonUtil.getObjFromStr(json, WxAuth.class) ;
		if (wxAuth.getErrcode()!= 0) {
			res.setRetCode(9);
			res.setRetMessage(wxAuth.getErrmsg());
			System.out.println("ERROR="+JsonUtil.getStriFromObj(wxAuth));
			return res;
		}
		res.setRetCode(0);
		res.setRetMessage("OK");
		wxAuth.setReg(0);
		req.getReqData().put("openid", wxAuth.getOpenid()) ;
		req.getReqData().put("unionid", wxAuth.getUnionid()) ;
		// 通过微信查询接口，获取数据库中用户信息
		PageData resTmp = service.getActiveUser(req) ;
		if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
			wxAuth.setReg(1);
			res.setData(resTmp.getPage().get(0));
			//获取商城token
			req.getReqData().put("phone", res.getData().get("phone")) ;
			PageData shopRes = userservice.getShopToken(req) ;
			String shopToken = (String) shopRes.getData().get("shopToken") ;
			res.setKey(shopToken);
		}
		res.setExtData(wxAuth);
		System.out.println(JsonUtil.getStriFromObj(res));
		return res ;
	}

	// 青呈时代小程序登录
	@RequestMapping(method = RequestMethod.GET, value = "/getOpenIdAndSessionKey_qcsd")
	public @ResponseBody  PageData getOpenID_qcsd(HttpServletRequest request,
												 HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		// 调用微信接口，获取用户信息
		DataReq req = req_Status.getReq() ;
		String code = (String) req.getReqData().get("code") ;
		String json = WeChatCommonUtils.getOpenIdAndSessionKey_qcsd(code);
		WxAuth wxAuth = (WxAuth)JsonUtil.getObjFromStr(json, WxAuth.class) ;
		if (wxAuth.getErrcode()!= 0) {
			res.setRetCode(9);
			res.setRetMessage(wxAuth.getErrmsg());
			System.out.println("ERROR="+JsonUtil.getStriFromObj(wxAuth));
			return res;
		}
		res.setRetCode(0);
		res.setRetMessage("OK");
		wxAuth.setReg(0);
		req.getReqData().put("openid", wxAuth.getOpenid()) ;
		req.getReqData().put("unionid", wxAuth.getUnionid()) ;
		// 通过微信查询接口，获取数据库中用户信息
		PageData resTmp = service.getActiveUser(req) ;
		if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
			wxAuth.setReg(1);
			res.setData(resTmp.getPage().get(0));
			//获取商城token
			req.getReqData().put("phone", res.getData().get("phone")) ;
			PageData shopRes = userservice.getShopToken(req) ;
			String shopToken = (String) shopRes.getData().get("shopToken") ;
			res.setKey(shopToken);
		}
		res.setExtData(wxAuth);
		System.out.println(JsonUtil.getStriFromObj(res));
		return res ;
	}

	// 微信小程序登录
	@RequestMapping(method = RequestMethod.GET, value = "/getOpenIdAndSessionKey_shore")
	@ResponseBody
	public PageData getOpenID(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
		PageData res = new PageData() ;
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode()) ;
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}
		DataReq req = req_Status.getReq() ;

		// 根据programCode获取微信小程序配置
		WechatProgramConfig wechatProgramConfig = wechatProgramConfigService.queryWechatProgramConfigByProgramCode((String) req.getReqData().get("merId"));
		if (ObjectUtil.isNull(wechatProgramConfig)) {
			res.setRetCode(9) ;
			res.setRetMessage("获取微信小程序配置信息失败!");
			return res;
		}

		// 调用微信接口，获取用户信息
		String code = (String) req.getReqData().get("code") ;
		String json = WeChatCommonUtils.getOpenIdAndSessionKey(code, wechatProgramConfig.getAppId(), wechatProgramConfig.getSecret());
		WxAuth wxAuth = (WxAuth)JsonUtil.getObjFromStr(json, WxAuth.class) ;
		if (wxAuth.getErrcode()!= 0) {
			res.setRetCode(9);
			res.setRetMessage(wxAuth.getErrmsg());
			System.out.println("ERROR="+JsonUtil.getStriFromObj(wxAuth));
			return res;
		}
		res.setRetCode(0);
		res.setRetMessage("OK");
		wxAuth.setReg(0);
		req.getReqData().put("openid", wxAuth.getOpenid()) ;
		req.getReqData().put("unionid", wxAuth.getUnionid()) ;
		// 通过微信查询接口，获取数据库中用户信息
		PageData resTmp = service.getActiveUser(req) ;
		if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
			wxAuth.setReg(1);
			res.setData(resTmp.getPage().get(0));
			//获取商城token
			req.getReqData().put("phone", res.getData().get("phone")) ;
			PageData shopRes = userservice.getShopToken(req) ;
			String shopToken = (String) shopRes.getData().get("shopToken") ;
			res.setKey(shopToken);
		}
		res.setExtData(wxAuth);
		System.out.println(JsonUtil.getStriFromObj(res));
		return res ;
	}

	//获取公司资料
	@RequestMapping(method = RequestMethod.POST, value = "/getCompanyIntroduce")
	public @ResponseBody  PageData getCompanyIntroduce(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.getCompanyIntroduce(req) ;
			return res;
	}
	
	//获取活动参与信息
	@RequestMapping(method = RequestMethod.POST, value = "/getActivityCount")
	public @ResponseBody  PageData getActivityCount(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			DataReq req = req_Status.getReq() ;
			res = service.getActivityCount(req) ;
			return res;
	}
	
	
	//参加自定义活动
	@RequestMapping(method = RequestMethod.POST, value = "/participateInActivity")
	public @ResponseBody  PageData participateInActivity(HttpServletRequest request,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
			DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
			PageData res = new PageData() ;
			if (req_Status.getRetCode() != 0) {
				res = new PageData();
				res.setRetCode(req_Status.getRetCode()) ;
				res.setRetMessage(req_Status.getRetMessage());
				return res;
			}	
			
			DataReq req = req_Status.getReq() ;
			
			PageData resTmp = service.isParticipateIn(req) ;
			if(resTmp.getRetCode()==0) {
				res.setRetCode(9);
				res.setRetMessage("您已参加该活动");
				return res ;
			}

			if(req!=null){
				res.setRetCode(9);
				res.setRetMessage("活动暂未开始！");
				return res ;
			}
			
			resTmp = service.getWxUser_Custom(req) ;
			if(resTmp.getRetCode()==0 && resTmp.getPage().size()>0) {
				req.getReqData().put("companyId", resTmp.getPage().get(0).get("companyId")) ;
				req.getReqData().put("phone", resTmp.getPage().get(0).get("phone")) ;
				req.getReqData().put("name", resTmp.getPage().get(0).get("name")) ;
				req.getReqData().put("companyName", resTmp.getPage().get(0).get("name")) ;
				req.getReqData().put("memo", resTmp.getPage().get(0).get("activeName")) ;
				req.getReqData().put("advertisementId", resTmp.getPage().get(0).get("uuid")) ;
			}else {
				//res.setRetCode(9);
				//res.setRetMessage("没有查询到活动");
				return resTmp ;
			}
			

			if(StringUtils.isEmpty((String)req.getReqData().get("companyId"))) {//如果找不到公司，默认本公司
				req.getReqData().put("companyId", "00") ;
			}
			
			if(StringUtils.isEmpty((String)req.getReqData().get("createUid"))) {//如果找不到公司，默认管理员
				req.getReqData().put("createUid", "131") ;
			}
			
			if(StringUtils.isEmpty((String)req.getReqData().get("cluesource"))) {//默认来源，07（其他）
				req.getReqData().put("cluesource", "07") ;
			}
			
			
			req.getReqData().put("active_info", req.getReqData()) ;
			
			UserInfo uinfo = new UserInfo();
			uinfo.setId((String)req.getReqData().get("createUid"));
			uinfo.setCompanyId((String)req.getReqData().get("companyId"));
			req.setUinfo(uinfo );
			req.getReqData().put("isLog", "0") ;
			
			req.getReqData().put("followupstatus", "01") ; //跟进状态
			req.getReqData().put("poolentrymode", "05") ; //线索池
			req.getReqData().put("culestate", "02") ; //线索状态，新线索
			req.getReqData().put("cluestage", "01") ; //跟进状态
			req.getReqData().put("cluesource", req.getReqData().get("cluesource")) ; //跟进状态
			req.getReqData().put("pooltype",  "其他") ;
			String xzqhdm = service_clue.getAreaCodeByPhone(req);
			req.getReqData().put("xzqhdm",xzqhdm) ;
			req.getReqData().put("cluename", req.getReqData().get("companyName")) ;
			req.getReqData().put("cluebelongtocompany", req.getReqData().get("companyId")) ;
			req.getReqData().put("projectname", req.getReqData().get("companyName")) ;
			req.getReqData().put("companycontact",req.getReqData().get("name")) ;
			req.getReqData().put("cluephone",req.getReqData().get("phone") ) ;
			
			req.getReqData().put("projectaddress",null) ;
			req.getReqData().put("numberhome", req.getReqData().get("province")) ;
			req.getReqData().put("uuid",null) ;
			res = service_clue.addClue(req) ;
			if(res.getRetCode()==0) {
				res.setRetMessage("恭喜您！您已成功参与活动，我司会在24小时内与您联系！");
			}
			return res;
	}

	//生成二维码
	@RequestMapping(method = RequestMethod.POST, value = "/createQRCode")
	@ResponseBody
	public PageData createQRCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		DataRes req_Status = DBUtils.setRemoteVisit(request, response, false);
		PageData res = new PageData();
		if (req_Status.getRetCode() != 0) {
			res = new PageData();
			res.setRetCode(req_Status.getRetCode());
			res.setRetMessage(req_Status.getRetMessage());
			return res;
		}

		DataReq req = req_Status.getReq();
		String page = ParameterUtil.getString(req, "page");
		String scene = ParameterUtil.getString(req, "scene");
		JSONObject param = new JSONObject();
		param.put("page", page);
		param.put("scene", scene);
		final ServletOutputStream os = response.getOutputStream();
		WeChatCommonUtils.creatQRCode(param, os);
		os.flush();
		os.close();
		response.setContentType("image/jpeg");
		res.setRetCode(0);
		return res;
	}
  	
}
